<?php
//购物车控制器类
class CartController extends CommonController{
	private $model;
	/**
	 * [__init 构造函数]
	 * @return [type] [description]
	 */
	public function __init(){
		parent::__init();
		$this->model=K("Cart");
	}
    /**
     * [index 购物车]
     * @return [type] [description]
     */
    public function index(){
        // 判断用户是否登录
        if (!isset($_SESSION['uid'])) {
            $this->success('未登录,请先登录后再结算',U('Login/index'));
        }
        $uid =$_SESSION['uid'];
        // 获取用户存放在购物车里面的表
        $cartData =$this->model->get_one_data(array('uid'=>$uid));
        // 判断购物车表是否有用户的信息,没有的话读SESSION;
        // p($cartData);die();
        if (!$cartData) {
            $cartData = cart::getAllData();
            if (!$cartData['total_rows']==0) {
                $goodsData =serialize($cartData);
                $this->model->add_data($goodsData);
            }
        }else{
            $cartData =unserialize($cartData['cartinfo']);
        }
        // p($cartData);
        // 分配变量
        $this->assign('cartData',$cartData);
        $this->dis('cart.html');
    }
    /**
     * [add 加入购物车]
     */
    public function add(){
        if (!IS_AJAX) $this->error('非法请求');
        $glid = Q('post.glid',0,"intval");
        $glidData =K("GoodsList")->get_one_data(array('glid'=>$glid));
        $gid = $glidData['gid'];
        $goodsData =K('Goods')->get_one_data(array('gid'=>$gid));
        $data = array(
            'id' => $glid, // 商品ID
            'name'=>$goodsData['gname'],// 商品名称
            'num' => 1, // 商品数量
            'price' => $glidData['tprice'], // 商品价格
            'options' => array(
                // 其他参数如价格、颜色、可以为数组或字符串
                'pic' => $goodsData['pic'],
                'gid' => $gid,
                'number' =>$glidData['number']
            )
        );
        cart::add($data); //添加到购物车
        if(isset($_SESSION['uid'])){
            $uid =$_SESSION['uid'];
            $goodsData = cart::getAllData();
            $goodsData =serialize($goodsData);
            if ($this->model->get_one_data(array('uid'=>$uid))) {
                $this->model->update_data(array('uid'=>$uid),$goodsData);
            }else{
                $this->model->add_data($goodsData);
            }   
        }
        echo 1;
        die();
    }
    /**
     * [get 异步获取购物车数据]
     * @return [type] [description]
     */
    public function get(){
        // 判断是否AJAX
        if (!IS_AJAX) $this->error('非法请求');
        // 获取用ID
        $uid = Q('post.uid',0,"intval");
        // 判断是否有传用户ID过来
        if ($uid) {
            $cartData =$this->model->get_one_data(array('uid'=>$uid));
            // 判断用户是否有购物车表里有信息,没有读SESSION
            if (!$cartData) {
                $cartData = cart::getAllData();
                if (!$cartData) {
                    echo 0;
                    die();
                }
                $str ='<ul>';
                foreach ($cartData['goods'] as $v) {
                   $str .='<li><a href="'.__ROOT__.'/index.php/Index/Goods/index/gid/'.$v['options']['gid'].'"><img src="'.__ROOT__.'/'.$v['options']['pic'].'"width="50" alt=""><span>'.$v['name'].'</span><span class="price">'.$v['price'].'元X'.$v['num'].'</span></a></li>';
                }
                $str .='</ul><p>共计'.$cartData["total_rows"].'件商品</p><p class="total">合计<span>'.$cartData["total"].'</span>元</p><a href="'.__ROOT__.'/index.php/Index/Cart/index" class="gocart">去购物车结帐</a>';
                echo $str;
                die();
            }
            // 把从购物车表里拿来的信息进行反序列化
            $cartData = unserialize($cartData['cartinfo']);
            $str ='<ul>';
            foreach ($cartData['goods'] as $v) {
               $str .='<li><a href="'.__ROOT__.'/index.php/Index/Goods/index/gid/'.$v['options']['gid'].'"><img src="'.__ROOT__.'/'.$v['options']['pic'].'"width="50" alt=""><span>'.$v['name'].'</span><span class="price">'.$v['price'].'元X'.$v['num'].'</span></a></li>';
            }
            $str .='</ul><p>共计'.$cartData["total_rows"].'件商品</p><p class="total">合计<span>'.$cartData["total"].'</span>元</p><a href="'.__ROOT__.'/index.php/Index/Cart/index" class="gocart">去购物车结帐</a>';
            echo $str;
            die();
        }
        // 如果是示登录用户,使用本地的SESSION
        $cartData = cart::getAllData();
        if (!$cartData) {
            echo 0;
            die();
        }
        $str ='<ul>';
        foreach ($cartData['goods'] as $v) {
           $str .='<li><a href="'.__ROOT__.'/index.php/Index/Goods/index/gid/'.$v['options']['gid'].'"><img src="'.__ROOT__.'/'.$v['options']['pic'].'"width="50" alt=""><span>'.$v['name'].'</span><span class="price">'.$v['price'].'元X'.$v['num'].'</span></a></li>';
        }
        $str .='</ul><p>共计'.$cartData["total_rows"].'件商品</p><p class="total">合计<span>'.$cartData["total"].'</span>元</p><a href="'.__ROOT__.'/index.php/Index/Cart/index" class="gocart">去购物车结帐</a>';
        echo $str;
        die();
    }


    /**
     * [change_num 异步修改购物车数据]
     * @return [type] [description]
     */
    public function change_num(){
        if (!IS_AJAX) $this->error('非法请求');
        $sid =Q('post.sid');
        $data=array(
            'sid'=>$sid,// 唯一sid，添加购物车时自动生成
            'num'=>Q('post.num',0,'intval'),
            'total'=>Q('post.price',0,'intval')
             );
        Cart::update($data);
        $cartData = cart::getAllData();
        $goodsData =serialize($cartData);
        $uid =$_SESSION['uid'];
        $this->model->update_data(array('uid'=>$uid),$goodsData);
        $total=$cartData["total"];
        echo json_encode($total);
        die();
    }
    /**
     * [del 删除购物车方法]
     * @return [type] [description]
     */
    public function del(){
        $sid =Q('get.sid');
        Cart::del($sid);
        $cartData = cart::getAllData();
        $uid =$_SESSION['uid'];
        if ($cartData['total_rows']==0) {
            // p($uid);die();
            $this->model->del_data(array('uid'=>$uid));
        }else{
            $goodsData =serialize($cartData);
            $this->model->update_data(array('uid'=>$uid),$goodsData);
        }
        $this->success('删除成功',U('Cart/index'));   
    }



}
